package org.codefinger.dao;

import java.util.Map;

import org.codefinger.dao.impl.DataAccesser;
import org.codefinger.dao.impl.QueryParam;
import org.codefinger.dao.impl.SQLVisitor;
import org.codefinger.dao.impl.sql.IValue;

public class InsertValues implements Inserter {

	private DataAccesser	dataAccesser;

	private IValue			iValue;

	public InsertValues(DataAccesser dataAccesser, String table, String setValues) {
		super();
		this.dataAccesser = dataAccesser;
		this.iValue = dataAccesser.createIValue(table, setValues);
	}

	@Override
	public long execute(Object... params) {
		QueryParam queryParam = new QueryParam(params);
		SQLVisitor visitor = new SQLVisitor(queryParam);
		iValue.visit(visitor);
		return dataAccesser.executeInsert(visitor.getSQL(), visitor.getParams());
	}

	@Override
	public long execute(Map<String, Object> params) {
		QueryParam queryParam = new QueryParam(params);
		SQLVisitor visitor = new SQLVisitor(queryParam);
		iValue.visit(visitor);
		return dataAccesser.executeInsert(visitor.getSQL(), visitor.getParams());
	}
	
	@Override
	public InserterChain putParam(String name, Object value) {
		final QueryParam queryParam = new QueryParam();
		queryParam.setNamedParam(name, value);
		return new InserterChain() {

			@Override
			public InserterChain putParam(String name, Object value) {
				queryParam.setNamedParam(name, value);
				return this;
			}

			@Override
			public long execute() {
				SQLVisitor visitor = new SQLVisitor(queryParam);
				iValue.visit(visitor);
				return dataAccesser.executeInsert(visitor.getSQL(), visitor.getParams());
			}

		};
	}
	
}
